// pages/Index/couponActivity/couponStatistics/couponStatistics.js
const controller=require("../../../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:{
      shopId:"",
      pageNumber:1,
      pageSize:20,
      couponId:"",
      keyword:""
    },
    name:"",
    pushNum:"",
    receiveNum:"",
    useNum:"",
    startTime:"",
    endTime:"",
    changeRate:"",
    failShow:false,
    oneButton:[{text:"关闭"}],
    selectShow:false,
    twoButton:[{text:"确认"},{text:"关闭"}],
    selectName:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'value.couponId':options.id,
      name:options.name,
      pushNum:options.pushNum,
      receiveNum:options.receiveNum,
      useNum:options.useNum,
      startTime:options.startTime,
      endTime:options.endTime,
      type:options.type
    })
    if(options.type==4){
      this.setData({
        jihuoBtn:true
      })
    }else{
      this.setData({
        jihuoBtn:false
      })
    }
    console.log(this.data.type)
    this.setData({
      'value.shopId':wx.getStorageSync('shopId')
    })
    let changeRate="";
    if(options.pushNum&&options.receiveNum&&options.useNum){
      changeRate=parseInt(options.receiveNum/options.pushNum*100) 
    }else{
      changeRate=parseInt(options.useNum/options.receiveNum*100) 
    }
    this.setData({
      changeRate:changeRate
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  //获取用户的活动礼包优惠券列表
  getUserList(){
    controller.accountActivityCouponPage(this.data.value).then(res=>{
      if(res.code==1){
        this.setData({
          list:res.data.list,
          lastPage:res.lastPage
        })
      }
    })
  },

   //获取子组件的传值
   myevent(e) {
    // 这里就是子组件传过来的内容了
    this.setData({
      'value.keyword':e.detail.params
    })
    this.getUserList()
  },

  //返回
  back(){
    wx.navigateBack({
      delta: 0,
    })
  },

  getMore(){
    let value=this.data.value;
    value.pageNumber=value.pageNumber+1;
    this.setData({
      value:value
    })
    if(this.data.lastPage!=true){
      controller.accountActivityCouponPage(value).then(res=>{
        let list=this.data.list;
        let list2=res.data.list;
        let cList=list.concat(list2)
        if(res.code==1){
          this.setData({
            list:cList,
            lastPage:res.data.lastPage
          })
        }
      })
    }
  },

  //关闭弹框
  closeBtn(){
    this.setData({
      failShow:false,
      selectShow:false
    })
  },

  //打开激活弹框
  activation(e){
    let state=e.currentTarget.dataset.state;
    let nick=e.currentTarget.dataset.nick;
    let couponAccountId=e.currentTarget.dataset.id
    this.setData({
      selectName:nick,
      couponAccountId:couponAccountId
    })
    if(state==5){
      this.setData({
        selectShow:true
      })
    }else{
      this.setData({
        failShow:true
      })
    }
  },

  //选择是否激活
  selectBtn(e){
    let index=e.detail.index;
    let shopId=wx.getStorageSync('shopId');
    let couponAccountId=this.data.couponAccountId;
    let that=this;
    if(index==0){
      controller.awaken({shopId:shopId,couponAccountId:couponAccountId}).then(res=>{
        if(res.code==1){
          wx.showToast({
            title: '激活成功',
            duration:2000
          })
          this.setData({
            'value.pageNumber':1
          })
          that.getUserList()
          that.closeBtn()
        }
      })
    }else{
      this.closeBtn()
    }
  }

})